/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.handler;

import net.cellobject.javacodeformatter.editor.PropertyCategory;
import net.cellobject.javacodeformatter.editor.PropertyElement;
import net.cellobject.javacodeformatter.editor.PropertyEntry;
import net.cellobject.javacodeformatter.editor.PropertyFile;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.AbstractOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;

/**
 * An operation for deleting properties appropriate for undo/redo.
 * 
 * @see DeletePropertyHandler
 */
public class DeletePropertyOperation extends AbstractOperation {
	private final PropertyElement[] elements;

	private PropertyElement[] parents;

	private int[] indexes;

	public DeletePropertyOperation(final PropertyElement[] elements) {
		super(getLabelFor(elements));
		this.elements = elements;
	}

	@Override
	public IStatus execute(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {
		// If a UI context has been provided,
		// then prompt the user to confirm the operation.
		if (info != null) {
			final Shell shell = (Shell)info.getAdapter(Shell.class);
			if (shell != null) {
				if (!MessageDialog.openQuestion(shell, "Remove properties", "Do you want to remove the currently selected properties?")) {
					return Status.CANCEL_STATUS;
				}
			}
		}
		// Perform the operation.
		return redo(monitor, info);
	}

	@Override
	public IStatus redo(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {
		// Perform the operation, providing feedback to the user
		// through the progress monitor if one is provided.
		parents = new PropertyElement[elements.length];
		indexes = new int[elements.length];
		if (monitor != null) {
			monitor.beginTask("Remove properties", elements.length);
		}
		final Shell shell = (Shell)info.getAdapter(Shell.class);
		shell.setRedraw(false);
		try {
			for (int i = elements.length; --i >= 0;) {
				parents[i] = elements[i].getParent();
				final PropertyElement[] children = parents[i].getChildren();
				for (int index = 0; index < children.length; index++) {
					if (children[index] == elements[i]) {
						indexes[i] = index;
						break;
					}
				}
				elements[i].removeFromParent();
				if (monitor != null) {
					monitor.worked(1);
				}
			}
		} finally {
			shell.setRedraw(true);
		}
		if (monitor != null) {
			monitor.done();
		}
		return Status.OK_STATUS;
	}

	@Override
	public IStatus undo(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {
		final Shell shell = (Shell)info.getAdapter(Shell.class);
		shell.setRedraw(false);
		try {
			for (int i = 0; i < elements.length; i++) {
				if (parents[i] instanceof PropertyCategory) {
					((PropertyCategory)parents[i]).addEntry(indexes[i], (PropertyEntry)elements[i]);
				} else {
					((PropertyFile)parents[i]).addCategory(indexes[i], (PropertyCategory)elements[i]);
				}
			}
		} finally {
			shell.setRedraw(true);
		}
		return Status.OK_STATUS;
	}

	private static String getLabelFor(final PropertyElement[] elements) {
		if (elements.length == 1) {
			final PropertyElement first = elements[0];
			if (first instanceof PropertyEntry) {
				final PropertyEntry propEntry = (PropertyEntry)first;
				return "Remove property " + propEntry.getKey();
			}
			if (first instanceof PropertyCategory) {
				final PropertyCategory propCat = (PropertyCategory)first;
				return "Remove category " + propCat.getName();
			}
		}
		return "Remove properties";
	}
}
